//
//  EYImageView.m
//  touch
//
//  Created by easy on 11-5-26.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "EYImageView.h"


@implementation EYImageView
@synthesize touchController;

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        // Initialization code
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)dealloc {
    [super dealloc];
}

- (EYTouchController *) touchController{
	if (!touchController) {
		touchController = [[EYTouchController alloc] init];
		touchController.delegate = self;
	}
	return touchController;
}

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
	[self.touchController touchesBegan:touches event:event];
	NSLog(@"current touch:%d",self.touchController.touchCount);
}
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
	[self.touchController touchesEnded:touches event:event];
}
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
	[self.touchController touchesMoved:touches event:event];
}
-(void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
	[self.touchController touchesCancelled:touches event:event];
}

-(void) didTouchBegan:(NSArray *)touches event:(UIEvent *)event controller:(EYTouchController *)controller{
	NSLog(@"begin touch:%d/%d",[touches count],controller.touchCount);
}
-(void) didTouchMoved:(NSArray *)touches event:(UIEvent *)event controller:(EYTouchController *)controller{
	NSLog(@"move touch:%d/%d",[touches count],controller.touchCount);
	if (controller.touchCount == 1) {//单点触摸
		EYTouch *touch = [controller touchAtIndex:0];
		//单点只有位移
		CGPoint offset = touch.offset;
		CGAffineTransform transform = CGAffineTransformTranslate(self.transform, offset.x, offset.y);
		self.transform = transform;
	}else if (controller.touchCount > 1) {//多点，只算两个点
		EYTouch *touch1 = [controller touchAtIndex:0];
		EYTouch *touch2 = [controller touchAtIndex:1];
		
		//CGPoint startPoint,endPoint;
		//startPoint.x = (touch2.start.x + touch1.start.x)/2;
		//startPoint.y = (touch2.start.y + touch2.start.y)/2;
		//endPoint.x = (touch2.current.x + touch1.current.x)/2;
		//endPoint.y = (touch2.current.y + touch2.current.y)/2;
		
		//NSLog(@"%.2f,%.2f,%.2f,%.2f",touch1.start.x,touch1.start.y,touch2.start.x,touch2.start.y);
		//NSLog(@"%.2f,%.2f,%.2f,%.2f",touch1.current.x,touch1.current.y,touch2.current.x,touch2.current.y);
		//位移 误差？
		//CGPoint offset = CGPointMake(endPoint.x - startPoint.x,endPoint.y - startPoint.y);
		//CGPoint offset = CGPointMake((touch2.offset.x + touch1.offset.x)/2,(touch2.offset.y + touch1.offset.y)/2);
		//NSLog(@"%.2f,%.2f",offset.x,offset.y);
		CGPoint offset = [touch1 translateTo:touch2];
		CGAffineTransform transform = CGAffineTransformTranslate(self.transform, offset.x, offset.y);
		
		//float vector1 = sqrt((touch1.start.x - touch2.start.x) * (touch1.start.x - touch2.start.x) + 
		//(touch1.start.y - touch2.start.y) * (touch1.start.y - touch2.start.y));
		//float vector2 = sqrt((touch1.current.x - touch2.current.x) * (touch1.current.x - touch2.current.x) + 
		//(touch1.current.y - touch2.current.y) * (touch1.current.y - touch2.current.y));
		//float scale = vector2 / vector1;
		
		float scale = [touch1 scaleTo:touch2];
		transform = CGAffineTransformScale(transform, scale, scale);
		
		//float vectorRadian1 = atan((touch2.start.y - touch1.start.y)/(touch2.start.x - touch1.start.x));
		//float vectorRadian2 = atan((touch2.current.y - touch1.current.y)/(touch2.current.x - touch1.current.x));
		
		//float angle = vectorRadian2 - vectorRadian1;
		float angle = [touch1 rotateTo:touch2];
		transform = CGAffineTransformRotate(transform, angle);
		self.transform = transform;
	}
}
-(void) didTouchEnded:(NSArray *)touches event:(UIEvent *)event controller:(EYTouchController *)controller{
	NSLog(@"end touch:%d/%d",[touches count],controller.touchCount);
	//self.transform = CGAffineTransformIdentity;
}
-(void) didTouchCancelled:(NSArray *)touches event:(UIEvent *)event controller:(EYTouchController *)controller{
	NSLog(@"cancel touch:%d/%d",[touches count],controller.touchCount);
}
@end
